/*
	a very simple queue implementation
	use std::list and add thread sync

	@author: Wolfhead
 */
#ifndef ACAPELLA_QUEUE_SIMPLE_QUEUE_HPP
#define ACAPELLA_QUEUE_SIMPLE_QUEUE_HPP

#include <acapella/synchronize/synchronize.h>
#include "queue_base.h"
#include <list>

namespace acapella
{
namespace queue
{

template<
	typename ElementType, 
	typename LockType = acapella::synchronize::_default_::spin_lock,
	typename SemType = acapella::synchronize::_default_::semaphore>
class SimpleQueue : public Queue<ElementType>
{
public:
	SimpleQueue(){};
	~SimpleQueue(){};

	void Send(const ElementType& element)
	{
		acapella::synchronize::scoped_spin_lock<LockType> scopedLock(lock_);

		list_.push_back(element);

		sem_.post();
	}

	_uint32_ Size()
	{
		acapella::synchronize::scoped_spin_lock<LockType> scopedLock(lock_);

		return list_.size();
	}

	int Recv(ElementType& value)
	{
		if (sem_.wait() != 0)
		{
			return -1;
		}

		acapella::synchronize::scoped_spin_lock<LockType> scopedLock(lock_);

		if (list_.size() == 0)
		{
			return -1;
		}

		value = list_.front();
		list_.pop_front();

		return 0;
	}

private:
	std::list<ElementType> list_;
	LockType lock_;
	SemType sem_;

};



}
}

#endif
